package com.ruipengkj.component;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;
/**
 * 订单处理按钮 ：结账，挂单，取单…………
 * 
 * **/
public class RPJButtonOrderDispose extends JButton {

	private static final long serialVersionUID = 1L;
	
	private TexturePaint paint = Util.createTexturePaint(ImageFactory.loadImage(ImageFactory.BTN_ORDER_BG).getImage());
	private TexturePaint paint2 = Util.createTexturePaint(ImageFactory.loadImage(ImageFactory.BTN_ORDER_BG2).getImage());
	private Image backgroundLeftImage = ImageFactory.loadImage(ImageFactory.BTN_ORDER_BG_LEFT).getImage();
	private Image backgroundRightImage = ImageFactory.loadImage(ImageFactory.BTN_ORDER_BG_RIGHT).getImage();
	
	private TexturePaint paint_select = Util.createTexturePaint(ImageFactory.loadImage(ImageFactory.BTN_ORDER_CLICK_BG).getImage());
	private Image backgroundLeftImage_select = ImageFactory.loadImage(ImageFactory.BTN_ORDER_CLICK_BG_LEFT).getImage();
	private Image backgroundRightImage_select = ImageFactory.loadImage(ImageFactory.BTN_ORDER_CLICK_BG_RIGHT).getImage();
	
	private ImageIcon icon;
	private String text;
	private int flash;
	
	private int type;//1：只画左边，0：左右不画，-1：只画右边
	public RPJButtonOrderDispose(String text, ImageIcon icon, int type){
//		super(text, icon);
		setContentAreaFilled(false); // 这一句非常重要, 否则父类还会绘制按钮的区域. 
		setCursor(Util.CURSOR_HAND);
		setHorizontalTextPosition(SwingConstants.RIGHT);
		setVerticalTextPosition(SwingConstants.CENTER);
		this.type = type;
		this.icon = icon;
		this.text = text;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		if (getModel().isRollover()) {
			//鼠标滑过
			if (type > 0) {
				g2d.drawImage(backgroundLeftImage_select, 0, 0, null);
			}
			g2d.setPaint(paint_select);
			int x = backgroundLeftImage_select.getWidth(null);
			int y = 0;
			int width = getWidth() - x - backgroundRightImage_select.getWidth(null);
			int height = getHeight();
			g2d.fillRect(x, y, width, height);
			if (type < 0) {
				g2d.drawImage(backgroundRightImage_select, x + width, 0, null);
			}
		} else {
			if (type > 0) {
				g2d.drawImage(backgroundLeftImage, 0, 0, null);
			}
			if (flash == 0 || ++flash % 2 == 0) {
				g2d.setPaint(paint);
			} else {
				g2d.setPaint(paint2);
			}
			int x = backgroundLeftImage.getWidth(null);
			int y = 0;
			int width = getWidth() - x - backgroundRightImage.getWidth(null);
			int height = getHeight();
			g2d.fillRect(x, y, width, height);
			if (type < 0) {
				g2d.drawImage(backgroundRightImage, x + width, 0, null);
			}
		}
		g2d.drawImage(icon.getImage(), 10, this.getHeight()/2-icon.getIconHeight()/2, null);
		g.setColor(Color.black);
		if (!text.equals("整单折"))
			g.drawString(text, 46, 22);
		else 
			g.drawString(text, 35, 22);
//		super.paintComponent(g);
	}
	
	public void setFlash(int flash) {
		this.flash = flash;
	}
}
